layui.use(['table'], function () {
    let $ = layui.$;
    let table = layui.table;

    /**
     * 系统管理--乡镇街道管理
     */
    var ZhArea = {
        tableId: "zhareaTable",
        condition: {
            zhareaName: ""
        }
    };


    ZhArea.initColumn = function () {
        return [[
            {title: '序号', type: 'numbers'},
            {field: 'name', align: "left", sort: true, title: '街道乡镇', minWidth: 120},
            {field: 'areaCode', align: "left", sort: true, title: '编号', minWidth: 120},
            {
                field: 'parentName', title: '上级单位',
                templet: function (d) {
                    var html='';
                    if(d.parentName){
                        html='<span class=" layui-btn-xs layui-bg-green">'+ d.parentName+' </span>'
                    }
                    return html;
                }
            },
            {align: 'center', toolbar: '#studentTableBar', title: '操作', minWidth: 145}
        ]];
    };


    var tableResult = table.render({
        elem: '#' + ZhArea.tableId,
        url: Feng.ctxPath + '/zharea/page',
        request: {pageName: 'pageNo', limitName: 'pageSize'}, //自定义分页参数
        height: "full-158",
        page:true,
        cellMinWidth: 100,
        cols: ZhArea.initColumn(),
        parseData: Feng.parseData
        ,autoSort: false
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        ZhArea.search();
    });

    /**
     * 点击查询按钮
     */
    ZhArea.search = function () {
        var queryData = {};
        queryData['name'] = $("#zhareaName").val();
        queryData['parentName'] = $("#zhareaParentName").val();
        table.reload(ZhArea.tableId, {
            where: queryData,
            page: {curr: 1}
        });
    };

    ZhArea.convertData=function(sourceList){
        var targetList = [];
        for (let i = 0; i < sourceList.length; i++) {
            var obj = {};
            obj.title=sourceList[i].name;
            obj.id=sourceList[i].id;
            targetList.push(obj);
        }
        return targetList;
    };

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        console.info(table);
        console.info(tableResult);
    });

    table.on('tool(zhareaTable)', function(obj){
        var that = this, data = obj.data;
        if(obj.event === 'parent'){
            // dropdown.render({
            //     elem: that
            //     ,show: true
            //     // ,data:District.convertData(District.parentList)
            //     ,isAllowSpread:false
            // });
        }
        console.log(obj);
    });

});